package com.moli.iov.alarm.remote.impl;

import com.moli.iov.alarm.model.dto.req.AlarmInfoReqDto;
import com.moli.iov.alarm.model.dto.req.AlarmInfoThirdDto;
import com.moli.iov.alarm.model.po.AlarmInfo;
import com.moli.iov.alarm.remote.IAlarmThirdRemoteService;
import com.moli.iov.alarm.service.IAlarmService;
import com.moli.iov.handle.alarm.enums.AlarmCodeEnum;
import com.moli.iov.handle.alarm.enums.AlarmStatusEnum;
import com.moli.iov.rest.RestResponse;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 报警业务  第三方 调用
 * </p>
 *
 * @author zql
 * @since 2019/9/27 10:29
 */
@Slf4j
@Service(group = "${service.group}")
public class AlarmThirdRemoteServiceImpl implements IAlarmThirdRemoteService {

  @Autowired
  private IAlarmService alarmService;

  /**
   * 失联车辆报警列表
   * @param alarmInfoThirdDto
   * @return
   */
  @Override
  public RestResponse<List<AlarmInfo>> listOfflineAlarms(
      AlarmInfoThirdDto alarmInfoThirdDto) {

    List<AlarmInfo> alarmInfos = alarmService
        .listAlarmInfoOfThird(assembleAlarmInfoReqDtoOfOffline(alarmInfoThirdDto));

    return RestResponse.success(alarmInfos);
  }

  /**
   * 组装参数
   * @param alarmInfoThirdDto
   * @return
   */
  private AlarmInfoReqDto assembleAlarmInfoReqDtoOfOffline(AlarmInfoThirdDto alarmInfoThirdDto){
    AlarmInfoReqDto alarmInfoReqDto = new AlarmInfoReqDto();
    alarmInfoReqDto.setAlarmCode(AlarmCodeEnum.OFF_LINE.getFlag());
    alarmInfoReqDto.setStatus(AlarmStatusEnum.ALARMING.getFlag());

    if(alarmInfoThirdDto == null){
      return alarmInfoReqDto;
    }
    alarmInfoReqDto.setVin(alarmInfoThirdDto.getVin());
    alarmInfoReqDto.setCompanyId(alarmInfoThirdDto.getCompanyId());
    alarmInfoReqDto.setLastReportBeginTime(alarmInfoThirdDto.getBeginTime());
    alarmInfoReqDto.setLastReportEndTime(alarmInfoThirdDto.getEndTime());
    alarmInfoReqDto.setLicense(alarmInfoThirdDto.getLicense());

    return alarmInfoReqDto;
  }

  /**
   * 里程异变报警列表
   * @param alarmInfoThirdDto
   * @return
   */
  @Override
  public RestResponse<List<AlarmInfo>> listMileageExAlarms(
      AlarmInfoThirdDto alarmInfoThirdDto) {

    List<AlarmInfo> alarmInfos = alarmService
        .listAlarmInfoOfThird(assembleAlarmInfoReqDtoMileageEx(alarmInfoThirdDto));

    return RestResponse.success(alarmInfos);
  }

  /**
   * 组装参数
   * @param alarmInfoThirdDto
   * @return
   */
  private AlarmInfoReqDto assembleAlarmInfoReqDtoMileageEx(AlarmInfoThirdDto alarmInfoThirdDto){
    AlarmInfoReqDto alarmInfoReqDto = new AlarmInfoReqDto();

    alarmInfoReqDto.setAlarmCode(AlarmCodeEnum.MILEAGE_EXCEPTION_CHANGE.getFlag());
    alarmInfoReqDto.setStatus(AlarmStatusEnum.ALARMING.getFlag());

    if(alarmInfoThirdDto == null){
      return alarmInfoReqDto;
    }
    alarmInfoReqDto.setVin(alarmInfoThirdDto.getVin());
    alarmInfoReqDto.setCompanyId(alarmInfoThirdDto.getCompanyId());
    alarmInfoReqDto.setBeginTime(alarmInfoThirdDto.getBeginTime());
    alarmInfoReqDto.setEndTime(alarmInfoThirdDto.getEndTime());
    alarmInfoReqDto.setLicense(alarmInfoThirdDto.getLicense());

    return alarmInfoReqDto;
  }

  /**
   * 围栏报警
   * @param alarmInfoThirdDto
   * @return
   */
  @Override
  public RestResponse<List<AlarmInfo>> listFenceAlarms(AlarmInfoThirdDto alarmInfoThirdDto) {

    List<AlarmInfo> alarmInfos = alarmService
        .listAlarmInfoOfThird(assembleAlarmInfoReqDtoFence(alarmInfoThirdDto));

    return RestResponse.success(alarmInfos);
  }

  /**
   * 组装参数
   * @param alarmInfoThirdDto
   * @return
   */
  private AlarmInfoReqDto assembleAlarmInfoReqDtoFence(AlarmInfoThirdDto alarmInfoThirdDto){
    AlarmInfoReqDto alarmInfoReqDto = new AlarmInfoReqDto();

    List<Integer> alarmCodes = Arrays.asList(AlarmCodeEnum.IN_FENCE.getFlag(), AlarmCodeEnum.OUT_FENCE.getFlag());
    alarmInfoReqDto.setAlarmCodes(alarmCodes);
    alarmInfoReqDto.setStatus(AlarmStatusEnum.ALARMING.getFlag());

    if(alarmInfoThirdDto == null){
      return alarmInfoReqDto;
    }
    alarmInfoReqDto.setVin(alarmInfoThirdDto.getVin());
    alarmInfoReqDto.setCompanyId(alarmInfoThirdDto.getCompanyId());
    alarmInfoReqDto.setBeginTime(alarmInfoThirdDto.getBeginTime());
    alarmInfoReqDto.setEndTime(alarmInfoThirdDto.getEndTime());
    alarmInfoReqDto.setLicense(alarmInfoThirdDto.getLicense());
    alarmInfoReqDto.setFenceIds(alarmInfoThirdDto.getFenceIds());

    return alarmInfoReqDto;
  }


}
